//###############
// FILE : SerchTab.java
// WRITER :
// Lior Zimmerman liorz06 066462300
// Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
//  Representation of the Search tab.
//###############
package oop.ex3.fileSearchView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import oop.ex3.fileSearchController.CanRunAction;
import oop.ex3.fileSearchController.ResetAction;
import oop.ex3.fileSearchController.SearchAction;
import oop.ex3.fileSearchController.SelectFilterFileAction;
import oop.ex3.fileSearchController.SelectWorkDirAction;
/**
 * Representation of the Search tab.
 */
public class SearchTab extends GUITab implements SearchTabInterface {
    private static final long serialVersionUID = 1L;


    //********************************************************************
	//  Data members :                                                   *
	//********************************************************************
	//Radio buttons.
    private JRadioButton _radioBtnFile;
    private JRadioButton _radioBtnText;
    //Buttons.   
    private JButton _btnWorkDir;
    private JButton _btnFilterFile;
    //Operation buttons.
    private JButton _btnReset;
    private JButton _btnRun;    
    //Set grid look
    private JLabel _lblFilterExp;
    private JTextField _txtWorkDir;
    private JTextField _txtFilterFile;
    private JTextField _txtFilterExp;
    private GridBagConstraints _layoutBag;
    //********************************************************************
	//  Finals :                                                         *
	//********************************************************************
	private final String FLTR_FRM_FILE_LBL = "Filter from file";
	private final String FLTR_FRM_TXT_LBL = "Filter from text";
	private final String BTN_WRK_DIR_LBL = "Select work directory";
	private final String BTN_FLTR_FILE = "Select filter file";
	private final String BTN_RST_TITLE = "Reset";
	private final String BTN_RUN_TITLE = "Run";
	private final String LBL_FLTR_EXP = "Filter expression";
	private final String FRAME_DIALOG_TITLE = "Search Dialog";

    //********************************************************************
	//  Methods :                                                        *
	//********************************************************************
    /**
     * Construct a new Search tab.
     * @param gui The main frame.
     */
    public SearchTab(GUIInitializer gui){
        super(gui);
        setLayout( new GridBagLayout());
        _layoutBag = new GridBagConstraints();
        _radioBtnFile = new JRadioButton(FLTR_FRM_FILE_LBL);
        _radioBtnText = new JRadioButton( FLTR_FRM_TXT_LBL);   
        _radioBtnText.setSelected(true);
        
        //add radio buttons to a group
        ButtonGroup groupBtnFilter = new ButtonGroup();        
        _btnWorkDir = new JButton(BTN_WRK_DIR_LBL);
        _btnFilterFile = new JButton(BTN_FLTR_FILE);
        //Add operation buttons.
        _btnReset = new JButton(BTN_RST_TITLE);
        _btnRun = new JButton(BTN_RUN_TITLE);
        
        //Set grid look
        _lblFilterExp = new JLabel(LBL_FLTR_EXP);
        _txtWorkDir = new JTextField();
        _txtFilterFile = new JTextField();
        _txtFilterExp = new JTextField();
        groupBtnFilter.add(_radioBtnFile);
        groupBtnFilter.add(_radioBtnText);  
        _btnFilterFile.setEnabled(false);
        _txtFilterFile.setEnabled(false);

        //Put all component in grid.
        setBorder( BorderFactory.createTitledBorder(FRAME_DIALOG_TITLE) );
        _layoutBag.gridx=0;
        _layoutBag.gridy=0;
        add(_radioBtnFile,_layoutBag);
        _layoutBag.gridx=1;
        _layoutBag.gridy=0;
        add(_radioBtnText,_layoutBag);
        _layoutBag.gridx=0;
        _layoutBag.gridy=1;
        _layoutBag.fill = GridBagConstraints.NONE;
        add(_btnWorkDir,_layoutBag);
        _layoutBag.gridx=1;
        _layoutBag.gridy=1;
        _layoutBag.fill = GridBagConstraints.HORIZONTAL;
        add(_txtWorkDir,_layoutBag);
        _layoutBag.gridx=0;
        _layoutBag.gridy=2;
        _layoutBag.fill = GridBagConstraints.NONE;
        add(_btnFilterFile,_layoutBag);
        _layoutBag.gridx=1;
        _layoutBag.gridy=2;
        _layoutBag.fill = GridBagConstraints.HORIZONTAL;
        add(_txtFilterFile,_layoutBag);
        _layoutBag.gridx=0;
        _layoutBag.gridy=3;
        _layoutBag.fill = GridBagConstraints.NONE;
        add(_lblFilterExp,_layoutBag);
        _layoutBag.gridx=1;
        _layoutBag.gridy=3;
        _layoutBag.fill = GridBagConstraints.HORIZONTAL;
        add(_txtFilterExp,_layoutBag);
        _layoutBag.gridx=0;
        _layoutBag.gridy=4;
        _layoutBag.fill = GridBagConstraints.HORIZONTAL;
        add(_btnRun,_layoutBag);
        _layoutBag.gridx=1;
        _layoutBag.gridy=4;
        _layoutBag.fill = GridBagConstraints.HORIZONTAL;
        _btnRun.setEnabled(false);
        add(_btnReset,_layoutBag);

    }
 
    /**
     * Sets tab action listener.
     */
    public void setActionListeners() {
    	_btnReset.addActionListener(new ResetAction(_guiPointer));
    	_btnRun.addActionListener(new SearchAction(_guiPointer));
    	_txtFilterExp.getDocument().addDocumentListener(
    	        new CanRunAction(_guiPointer));
    	
    	_txtFilterFile.getDocument().addDocumentListener(
    	        new CanRunAction(_guiPointer));
    	_txtWorkDir.getDocument().addDocumentListener(
    	        new CanRunAction(_guiPointer));
    	_radioBtnFile.addActionListener(new CanRunAction(_guiPointer));
    	_radioBtnText.addActionListener(new CanRunAction(_guiPointer));
    	_btnWorkDir.addActionListener(new SelectWorkDirAction(_guiPointer,
    	        JFileChooser.DIRECTORIES_ONLY));
    	_btnFilterFile.addActionListener(new SelectFilterFileAction(
    	        _guiPointer,JFileChooser.FILES_ONLY));
    }
    /**
     * Sets Filter-From-File button status.
     * @param val Button status.
     */
    public void btnFilterFileSetEnabled(boolean val) {
        _btnFilterFile.setEnabled(val);
    }
    /**
     * Sets Reset button status.
     * @param val Button status.
     */
    public void btnResetSetEnabled(boolean val) {
        _btnReset.setEnabled(val);
    }
    /**
     * Sets Run button status.
     * @param val Button status.
     */
    public void btnRunSetEnabled(boolean val) {
        _btnRun.setEnabled(val);
    }
    /**
     * Sets Work-Directory button status.
     * @param val Button status.
     */
    public void btnWorkDirSetEnabled(boolean val) {
        _btnWorkDir.setEnabled(val);
    }
	/**
	 * Returns true if Filter-From-File button is selected, 
	 * otherwise false.
	 * @return True if Filter-From-File button is selected, 
	 * otherwise false.
	 */
    public boolean rdoFilterFromFileSelected() {
        return _radioBtnFile.isSelected();
    }
    /**
	 * Returns true if Filter-From-Text button is selected, 
	 * otherwise false.
	 * @return True if Filter-From-Text button is selected, 
	 * otherwise false.
	 */
    public boolean rdoFilterFromTextSelected() {
        return _radioBtnText.isSelected();
    }
    /**
     * Returns the text from filter expression.
     * @return Text from filter expression.
     */
    public String txtFilterExpGetText() {
        return _txtFilterExp.getText();
    }
    /**
     * Sets the status of the filter expression button.
     * @param val button status.
     */
    public void txtFilterExpSetEnabled(boolean val) {
        _txtFilterExp.setEnabled(val);
    }
    /**
     * Sets Filter-Expression text.
     * @param text Text to be set.
     */
    public void txtFilterExpSetText(String text) {
        _txtFilterExp.setText(text);
    }
    /**
     * Returns the Filter-From-File text.
     * @return The Filter-From-File text.
     */
    public String txtFilterFileGetText() {
        return _txtFilterFile.getText();
    }
    /**
     * Sets Filter-From-File button status.
     * @param val Button status.
     */
    public void txtFilterFileSetEnabled(boolean val) {
        _txtFilterFile.setEnabled(val);
    }
    /**
     * Sets the Filter-From-File text.
     * @param text Text to be set at the filter from file.
     */
    public void txtFilterFileSetText(String text) {
        _txtFilterFile.setText(text);
    }
    /**
     * Returns Work-Directory text.
     * @return Work-Directory text.
     */
    public String txtWorkDirGetText() {
        return _txtWorkDir.getText();
    }
    /**
     * Sets Work-Directory button status.
     * @param val Button status.
     */
    public void txtWorkDirSetEnabled(boolean val) {
        _txtWorkDir.setEnabled(val);        
    }
    /**
     * Sets Work-Directory text.
     * @param text The text to be set.
     */
    public void txtWorkDirSetText(String text) {
        _txtWorkDir.setText(text);
    }
    /**
     * Returns true if the filter-from-file button is enabled.
     * @return True if the filter-from-file button is enabled, 
     * false otherwise.
     */
    public boolean txtFilterFileIsEnabled(){
        return _txtFilterFile.isEnabled();
    }
    /**
     * Return true if the work-directory button is enabled.
     * @return True if the work-directory button is enabled, 
     * false otherwise.
     */
    public boolean txtWorkDirIsEnabled(){
        return _txtWorkDir.isEnabled();
    }
    /**
     * Return true if the filter-expression button is enabled.
     * @return True if the filter-expression button is enabled, 
     * false otherwise.
     */
    public boolean txtFilterExpIsEnabled(){
        return _txtFilterExp.isEnabled();
    }
    /**
    * Sets the value of the Filter-From-File button status.
    * @param val Button status.
    */
    public void rdoSetSelectedFilterFromFile(boolean val){
        _radioBtnFile.setSelected(val);
    }
    /**
    * Sets the value of the Filter-From-Text button status.
    * @param val Button status.
    */
    public void rdoSetSelectedFilterFromText(boolean val){
        _radioBtnText.setSelected(val);
    }
}
